package xj.graph2d.app.prop;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import xj.graph2d.Arrow;
import xj.graph2d.ArrowAttr;
import xj.graph2d.DrawAttr;

public class TailArrowIcon implements ArrowIcon {

  protected Dimension dim = new Dimension(30, 16);

  protected Arrow arrow;

  protected ArrowAttr.ArrowType type = ArrowAttr.ArrowType.NoArrow;

  protected int width = 12;

  protected int height = 12;

  public TailArrowIcon() {
    arrow = new Arrow(type, width, height);
    arrow.setAngle(0);
  }

  public void setArrowType(ArrowAttr.ArrowType type) {
    this.type = type;
    arrow.setType(type);
  }

  public ArrowAttr.ArrowType getArrowType() {
    return type;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	RenderingHints.VALUE_ANTIALIAS_ON);
    Color oldColor = g2.getColor();
    //
    // g2.setColor(Color.white);
    // g2.fillRect(x, y, getIconWidth(), getIconHeight());
    //
    //g2.setColor(Color.black);
    g2.setColor(DrawAttr.defaultPenColor);
    float yy = y + dim.height / 2f;
    arrow.setPosition(x, yy);
    Point2D p = arrow.getTailPosition();
    arrow.draw(g2);
    g2.draw(new Line2D.Float(x + dim.width, yy, (float) p.getX(), yy));
    g2.setColor(oldColor);
  }

  public int getIconWidth() {
    return dim.width;
  }

  public int getIconHeight() {
    return dim.height;
  }

}
